package com.hbzjt2012.lesson03;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindowListener {
    public static void main(String[] args) {
        new WindowFrame();
    }
}

class WindowFrame extends Frame{
    public WindowFrame(){
        setBackground(Color.green);
        setBounds(100,100,200,200);
        setVisible(true);
        // addWindowListener(new MyWindowListener());

        this.addWindowListener(new WindowAdapter() {
            // 匿名内部类

            // 关闭窗口
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("windowClosing");
                System.out.println("你点击了关闭按钮");
                System.exit(0);
            }

            @Override
            public void windowOpened(WindowEvent e) {
                System.out.println("windowOpened");
            }

            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("windowClosed");
            }

            // 激活窗口
            @Override
            public void windowActivated(WindowEvent e) {
                WindowFrame frame = (WindowFrame) e.getSource();
                frame.setTitle("被激活了！");
                System.out.println("windowActivated");
            }
        });
    }

    class MyWindowListener extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            setVisible(false); // 隐藏窗口，通过按钮隐藏当前窗口
            System.exit(0); // 正常退出
        }
    }
}
